// pages/user_fuli_share/fulishare.js
var app = getApp();
Page({ 

  /**
   * 页面的初始数据
   */
  data: {
    logo: '',
    ratio: app.phoneInfo.ratio,
    alertType: false,
    second: 10,
    submitType: true,
    local_policy_id: '',
    disabled: true,
    name: '',
    img1: {},
    img2: {},
    img3: {},
    phone: '',
    code: '',
    codeText: '获取验证码',
    isGetCode: true,
    codeColor: '#e63939',
    shareTempFilePath: '',
    showDialog: false,
    sverType: false,
    shareJson: {
      logo: '',
      wx_head: '',
      wx_name: '',
      start_date: '',
      end_date: '',
      money: 0,
      desc: '',
      id_number: 0
    }
  },

  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    this.setData({
      'name': options.name,
      'shareJson.logo': options.logo,
      'shareJson.wx_head': options.wx_head,
      'shareJson.wx_name': options.wx_name,
      'shareJson.start_date': options.start_date,
      'shareJson.end_date': options.end_date,
      'shareJson.money': options.money, 
      'shareJson.id_number': options.id_number,
      'shareJson.desc': options.desc
    })

    this.getImage() ;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },


  /**
   * 获取图片
   */
  getImage: function () {
    wx.getImageInfo({
      src: this.data.shareJson.logo,
      success: img1 => {
        wx.getImageInfo({
          src: 'https://zsy.api.oucnet.com/statics/zsy/resourse/img/app_qrcode.png',
          success: img2 => {
            wx.getImageInfo({
              src: this.data.shareJson.wx_head,
              success: img3 => {
                // this.drawImg(img1, img2, img3);
                this.setData({
                  'img1': img1,
                  'img2': img2,
                  'img3': img3,
                })  
              }
            })
          },
          fail: () => {
            wx.hideLoading();
          }
        })
      },
      fail: () => {
        wx.hideLoading();
      }
    });
  },

  /**
   * 保存分享福利券
   */
  submit:function(){
    wx.showLoading({
      title: '保存中',
    });

    let _this = this ;
    let interval = setInterval(function(){
      if (_this.data.img1.path != undefined) {
        clearInterval(interval);
        _this.drawImg(_this.data.img1, _this.data.img2, _this.data.img3);
      }
    },500);
    
    
  },
 
  /**
   * canvas绘制
   */
  drawImg: function (img1, img2, img3) {
    var context = wx.createCanvasContext('share');
    context.fillStyle = '#F1F1F1';
    context.shadowColor = "#E5E5E5";  
    context.fillRect(0, 0, 620 / this.data.ratio, 1065 / this.data.ratio);

    let canvasWidthPx = 620 / this.data.ratio;
    let canvasHeightPx = 1065 / this.data.ratio;
    context.drawImage("/images/fuli-share-bg.png", 0, 0, canvasWidthPx, canvasHeightPx);

    let img1X = (620 - img1.width) / this.data.ratio / 2;
    let img1Y = 40 / this.data.ratio;
    context.drawImage(img1.path, img1X, img1Y, img1.width / this.data.ratio, img1.height / this.data.ratio);

    context.setFontSize(72 / this.data.ratio);
    context.setTextAlign('right');
    context.setFillStyle('#ff3333');
    context.fillText(this.data.shareJson.money, (420 - 65) / this.data.ratio, (415 - 67) / this.data.ratio);

    let img2X = (262 - 65) / this.data.ratio;
    let img2Y = (513 - 67) / this.data.ratio;
    context.drawImage(img2.path, img2X, img2Y, 225 / this.data.ratio, 225 / this.data.ratio);

    context.setFontSize(24 / this.data.ratio);
    context.setTextAlign('left');
    context.setFillStyle('#666666');
context.fillText('有效期：' + this.data.shareJson.start_date + ' - ' + this.data.shareJson.end_date, (100 - 65) / this.data.ratio, (890 - 67) / this.data.ratio);
context.fillText('适用说明：' + this.data.shareJson.desc, (100 - 65) / this.data.ratio, (930-67) / this.data.ratio);

    context.save();
    context.beginPath();
    context.arc((174-65) / this.data.ratio, (1060-67) / this.data.ratio, 50 / this.data.ratio, 0, Math.PI * 2, false);
    context.setStrokeStyle('#e0e0e0');
    context.stroke();
    context.clip();
    context.drawImage(img3.path, (124-65) / this.data.ratio, (1010-67) / this.data.ratio, 100 / this.data.ratio, 100 / this.data.ratio);
    context.restore();

    context.setFontSize(32 / this.data.ratio);
    context.setTextAlign('left');
    context.setFillStyle('#ffffff');
    context.fillText('本优惠由' + this.data.shareJson.wx_name + '为你争取', (244 - 65) / this.data.ratio, (1050-65) / this.data.ratio);
    context.setFontSize(24 / this.data.ratio);
    context.fillText('顾问编号：' + this.data.shareJson.id_number, (244-65) / this.data.ratio, (1100-67) / this.data.ratio);

    context.draw(false, this.getTempFilePath);
  },

  /**
   * 获取临时路径
   */
  getTempFilePath: function () {
    wx.canvasToTempFilePath({
      canvasId: 'share',
      success: res => {
        this.setData({
          shareTempFilePath: res.tempFilePath
        })
        this.saveImageToPhotosAlbum();
      }
    })
  },

  /**
   * 保存相册
   */
  saveImageToPhotosAlbum: function () {
    wx.hideLoading();
    var that = this;
    if (!this.data.shareTempFilePath) {
      wx.showModal({
        title: '提示',
        content: '图片绘制中，请稍后重试',
        showCancel: false
      })
    }
    wx.getSetting({
      success(res) {
        console.log(res);
        if (!res.authSetting['scope.writePhotosAlbum']) {
          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success() {
              that.save();
            },
            fail(err) {
              console.log(err);
              that.setData({
                'showDialog': true
              })
            }
          })
        } else {
          that.save();
        }
      }
    })
  },

  save: function () {
    wx.saveImageToPhotosAlbum({
      filePath: this.data.shareTempFilePath,
      success: (res) => {
        this.setData({
          alertType: true
        })
        this.waitTime();
      },
      fail: (err) => {
        console.log(err);
      }
    })
  },

  /**
   * 倒计时
   */
  waitTime: function () {
    let s = this.data.second;
    var timer = setInterval(() => {
      s--;
      if (s == 0) {
        clearInterval(timer);
        timer = null;
        this.setData({
          submitType: false
        })
      } else {
        this.setData({
          second: s
        });
      }
    }, 1000);

    // setTimeout(this.waitTime, 1000);
  },

  /**
   * 关闭弹窗
   */
  closeAlert: function () {
    this.setData({
      alertType: false,
      submitType: true,
      sverType: true,
      second: 10
    })

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})